package service;

import JDBC.DBUtils;
import method.ItemInformation;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
/**
 * @author Gao Shiwei
 * user can search items by item type, name or id
 */
@WebServlet(name = "FindItem")
public class SearchItem extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.addHeader("Access-Control-Allow-Credentials", "true");
        System.out.println("zzzz");
        String ItemID = req.getParameter("ItemID");
        String ItemName = req.getParameter("ItemName");
        String ItemType = req.getParameter("ItemType");
        String ItemTypeNumber = req.getParameter("ItemTypeNumber");
        int TypeNumber=0;

        ItemInformation[] answer;

        String str=req.getParameter("ItemType");
        System.out.println(str);
        //String newStr="a";
        String[] data=null;

        if(str!=null) {
            data = str.split(",");
            TypeNumber = data.length;
        }
        System.out.println(TypeNumber+"zezx");
        if (TypeNumber == 0) {
            System.out.println("66666666");
            if (ItemID != null) {
                System.out.println("Use ItemID");
                answer = DBUtils.QueryItemInformation("ItemID", ItemID);
            } else if (ItemName != null) {
                System.out.println("Use ItemName");
                answer = DBUtils.QueryItemNameInformation(ItemName);
            } else {
                System.out.println("Use ItemType");
                answer = DBUtils.QueryItemInformation("ItemType", ItemType);
            }
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }

            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();

        } else if (TypeNumber == 1) {
            answer = DBUtils.QueryItemInformation("ItemType", data[0]);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();


        } else if (TypeNumber == 2) {
            answer = DBUtils.QueryItemInformation(data,2);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");

            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==3) {
            answer = DBUtils.QueryItemInformation(data,3);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==4) {
            answer = DBUtils.QueryItemInformation(data,4);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==5) {
            answer = DBUtils.QueryItemInformation(data,5);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==6) {
            answer = DBUtils.QueryItemInformation(data,6);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==7) {
            answer = DBUtils.QueryItemInformation(data,7);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==8) {
            answer = DBUtils.QueryItemInformation(data,8);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==9) {
            answer = DBUtils.QueryItemInformation(data,9);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }else if(TypeNumber==10) {
            answer = DBUtils.QueryItemInformation(data,10);
            JsonObject Container = new JsonObject();

            JsonArray array = new JsonArray();

            JsonObject oneItem = new JsonObject();

            for (int i = 0; i < answer.length; i++) {
                oneItem.addProperty("ItemID", answer[i].getItemID());
                System.out.println(answer[i].getItemID() + " v " + i);
                oneItem.addProperty("ItemName", answer[i].getItemName());
                oneItem.addProperty("ItemPrice", answer[i].getItemPrice());
                oneItem.addProperty("ItemType", answer[i].getItemType());
                oneItem.addProperty("ItemDescription", answer[i].getItemDescription());
                oneItem.addProperty("ItemQuantity", answer[i].getItemQuantity());
                String picture = DBUtils.QueryItemPicture(answer[i].getItemID());
                if (picture != null)
                    picture.replace("\\", "\\\\");
                oneItem.addProperty("Picture", picture);
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(answer[i].getCreateTime()));
                array.add(oneItem);
                oneItem = new JsonObject();
                System.out.println(array + "\n");
            }
            Container.add("Item", array);

            PrintWriter out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }
    }
}